<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Function_demo</title>
</head>
<body>

</body>
<script>
    function add(a,b){
        return a+b;
    }
    console.log(add(1,2));
    console.log(add(1));

    function foo(){
        var x =1;
        x += 1;
    }

    function bar(){
        var a =1;
        function b() {
            var y = a + 1;

        }
    }

    // 唯一的全局变量
    let MYAPP = {};

    // 其他变量
    MYAPP.name = 'MYAPP';
    MYAPP.version = '1.0.0';
    MYAPP.add = function(a,b){
        return a+b;
    }

    const PI = 3.14;
    let X =1;

    let PERSON = {
        name:'张三',
        age:18,
        gender:'男',
        password:'<PASSWORD>',
        show:function(){
            console.log(this.name);
        }
    }

    let xiaoming = {
        name:'小明',
        age:10,
        gender:'男',
        age: function(){
            let y = new Date().getFullYear();
            return y-this.age;
        }
    }

    function getAge(){
        let y = new Date().getFullYear();
        return y-this.birth;
    }
    let xiaoming = {
        name:'小明',
        age:10,
        gender:'男',
        birth:2010,
        age: getAge     //  绑定到对象上的函数 方法
    };

    let xiaoming = {
        name:'小明',
        age:10,
        gender:'男',
        birth:2010,
        age: function(){
            let that = this;
            function getAgeFromBirth(){
                let y  = new Date().getFullYear();
                return y-that.birth;
            }
            return getAgeFromBirth();
        }
    };
    xiaoming.age();

    function getAge(){
        let y = new Date().getFullYear();
        return y-this.birth;
    }
    let xiaoming = {
        name:'小明',
        age:10,
        gender:'男',
        birth:2010,

    }
    xiaoming.age();
    getAge.apply(xiaoming,[]);

    //  装饰器  apply
     let count = 0;
     let oldParseInt = parseInt;  // 保存旧的方法

    window.parseInt = function(){
        count +=1;
        return oldParseInt.apply(this,arguments);   // 调用原函数
    }
    parseInt('10');
    parseInt('20');
    parseInt('30');
    console.log('count =' + count);
</script>
</html>